<?php
/**
 * 收藏
 */
namespace App\Service;

use App\Models\EwMemberSubs;
use App\Models\EwDics;
use App\Models\EwCats;

class MemberSubsService extends BaseService
{
	/**
	 * [getList app 会员订阅列表]
	 * @param  [type]  $params [description]
	 * @param  boolean $count  [description]
	 * @return [type]          [description]
	 */
	public static function getList($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(['s.id,s.member_id,cate_id as thumb,
			(SELECT c.name AS cate_name FROM App\Models\EwCats c where s.cate_id = c.id) AS cate_name,
			s.region,s.layout,s.price']);
		$builder->addFrom("App\Models\EwMemberSubs", 's');

		//member_id
		if (isset($params->member_id) && $params->member_id)
			$builder->andwhere("s.member_id = ".$params->member_id);

		$sql = " s.id DESC, s.create_time DESC";
		//order_by 排序
		if (isset($params->order_by))
			$sql = parent::orderBy($params->order_by, $table = "s");

		$builder->orderBy($sql);
		if ($count)
			return $builder->getQuery()->execute()->count();

		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();
		return $result->execute();
	}

	public static function getSearch($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(['AS id,s.member_id,(SELECT c.name AS cate_name FROM App\Models\EwCats c where s.cate_id = c.id) AS cate_name,s.area,s.layout,s.price,s.create_time']);
		$builder->addFrom("App\Models\EwMemberSubs", 's');
		// $builder->leftJoin("App\Models\EwMember", 's.member_id = m.id' , 'm');
		// $builder->leftJoin("App\Models\EwUser", 's.member_id = u.id' , 'u');

		//area
		if (isset($params->area) && $params->area )
			$builder->andwhere("s.area LIKE '%" . $params->area . "%'");

		$sql = " s.id DESC, s.create_time DESC";
		//order_by 排序
		if (isset($params->order_by))
			$sql = parent::orderBy($params->order_by, $table = "s");

		$builder->orderBy($sql);
		if ($count)
			return $builder->getQuery()->execute()->count();

		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();
		return $result->execute();
	}

	public static function getInfo($id = 0)
	{

		$info = EwMemberSubs::findFirst($id);

		if (!$info)
			return NULL;

		$cate = EwCats::findFirst($info->cate_id);

		$raw = new \stdClass();
		$raw->cate_name = $cate->name;

		foreach ($info as $key => $value) {
			$raw->$key = $value;
		}

		return $raw;
	}

	public static function count()
	{
		return EwMemberSubs::count();
	}

	public static function setSubs($params)
	{
		$raw = new \stdClass();

		$raw->member_id = $params->member_id;
		$raw->cate_id   = $params->cate_id;
		$dics_ids  = $params->dics_ids;
		$dics_arr  = array_values($dics_ids);
		sort($dics_arr);
		if (count($dics_arr) <=0)
			return NULL;

		$data = EwDics::find(
		    [
		    	"columns" =>"id,pid,name",
		        'id IN ({letter:array})',
		        'bind' => ['letter' => $dics_arr]
		    ]
		)->toArray();

		$arr = [];
		$arr["dics_ids"] = implode(",", $dics_arr);

		//不能重复添加相同类型
		$info =EwMemberSubs::findFirst(['conditions' => 'member_id = ' . $params->member_id .' AND dics_ids = "'. $arr['dics_ids'] .'"']);
		if ($info)
			return NULL;

		foreach ($data as $key => $value) {

			if($value["pid"] == 2 || $value["pid"] == 102){//price (单价元)(万)
				$unit = "元/㎡";
				if ($value['pid'] == 102)
					$unit = "万"; 

				 if (mb_substr($value["name"],-2,2,"UTF-8") == "以下"){
				 	$arr["price"]["min"] = "min";
				 	$arr["price"]["min_price"] = $value["name"];
				 }else if (mb_substr($value["name"],-2,2,"UTF-8") == "以上"){
				 	$arr["price"]["max"] = "max";
				 	$arr["price"]["max_price"] = $value["name"];
				 }else{
				 	$price = explode("-",$value["name"]);
				 	$arr["price"][] = intval($price[0]);
				 	$arr["price"][] = intval($price[1]);
				 }

			}else if($value["pid"] == 3 || $value["pid"] == 103){//layout

				$arr["layout"] = isset($arr["layout"])?$arr["layout"]:"";
				$arr["layout"] .= $value["name"].",";

			}else if($value['pid'] == 4 || $value["pid"] == 104){//area

				if (mb_substr($value["name"],-2,2,"UTF-8") == "以下"){
				 	$arr["area"]["min"] = "min";
				 	$arr["area"]["min_area"] = $value["name"];
				 }else if (mb_substr($value["name"],-2,2,"UTF-8") == "以上"){
				 	$arr["area"]["max"] = "max";
				 	$arr["area"]["max_area"] = $value["name"];
				 }else{
				 	$area = explode("-",$value["name"]);
				 	$arr["area"][] = intval($area[0]);
				 	$arr["area"][] = intval($area[1]);
				 }
			}else{
				$region = EwDics::findFirst(["conditions" => "pid = 1 AND id = ".$value["pid"]]);
				if ($region){
					$arr["region"] = isset($arr["region"])?$arr["region"]:"";
					$arr["region"] .= $value["name"].",";
				}else{
					$arr["other"] = isset($arr["other"])?$arr["other"]:"";
					$arr["other"] .= $value["name"].",";
				}
			}
		}

		if (isset($arr["price"])){
			$arr["price"]["min"] = isset($arr["price"]["min"])?$arr["price"]["min"]:min($arr["price"]);
			$arr["price"]["max"] = isset($arr["price"]["max"])?$arr["price"]["max"]:max($arr["price"]);

			if ($arr["price"]["min"] == "min" && $arr["price"]["max"] =="max"){//选了以下 且选了以上
				$arr["price"] = "价格不限";
			}else if ($arr["price"]["min"] == "min"){//选了以下
				if ($arr["price"]["max"] && $arr["price"]["max"] !="min"){
					$arr["price"] = $arr["price"]["max"] . $unit . "以下";
				}else{
					$arr["price"] = $arr["price"]["min_price"];
				}
			}else if ($arr["price"]["max"] == "max") {
				if ($arr["price"]["max_price"] && isset($arr["price"]["0"])){
					$arr["price"] = $arr["price"]["0"] . $unit . "以上";
				}else{
					$arr["price"] = $arr["price"]["max_price"];
				}
			}else{
				$arr["price"] = $arr["price"]["min"] . "-" . $arr["price"]["max"] . $unit;			
			}
		}

		if (isset($arr["area"])){
			$arr["area"]["min"] = isset($arr["area"]["min"])?$arr["area"]["min"]:min($arr["area"]);
			$arr["area"]["max"] = isset($arr["area"]["max"])?$arr["area"]["max"]:max($arr["area"]);

			if ($arr["area"]["min"] == "min" && $arr["area"]["max"] =="max"){//选了以下 且选了以上
				$arr["area"] = "面积不限";
			}else if ($arr["area"]["min"] == "min"){//选了以下
				if ($arr["area"]["max"] && $arr["area"]["max"] !="min"){
					$arr["area"] = $arr["area"]["max"] . "㎡以下";
				}else{
					$arr["area"] = $arr["area"]["min_area"];
				}
			}else if ($arr["area"]["max"] == "max") {
				if ($arr["area"]["max_area"] &&  isset($arr["area"]["0"])){
					$arr["area"] = $arr["area"]["0"] . "㎡以上";
				}else{
					$arr["area"] = $arr["area"]["max_area"];
				}
			}else{
				$arr["area"] = $arr["area"]["min"] . "-" . $arr["area"]["max"] . "㎡";		
			}
		}
		$arr["region"] = isset($arr["region"])?substr($arr["region"],0,strlen($arr["region"])-1) :"区域不限";
		$arr["area"]   = isset($arr["area"])?$arr["area"] :"面积不限";
		$arr["layout"] = isset($arr["layout"])?substr($arr["layout"],0,strlen($arr["layout"])-1):"户型不限";
		$arr["price"]  = isset($arr["price"])?$arr["price"] :"价格不限";
		
		$arr["other"] = isset($arr["other"])?substr($arr["other"],0,strlen($arr["other"])-1) :"";
		foreach ($arr as $key => $value) {
			$raw->$key = $value;
		}	

		return self::save($raw);
	}

	/**
	 * [save 点击收藏]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$model = new EwMemberSubs;
		if (isset($data->id) && $data->id)
			$model = EwMemberSubs::findFirst($data->id);

		return $model->save((array)$data);
	}

	/**
	 * [delete 彻底删除]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwMemberSubs::findFirst($id);

		if ($row)
			return $row->delete();

		return NULL;
	}
}